package com.csust.automaticteach.controller;

/**
 * @ClassNameAiController
 * @Description TODO
 * @Author钰玟
 * @Date2025/6/17 下午3:33
 * @Version 1.0
 **/

import com.csust.automaticteach.app.student.OnlineLearnApp;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.tool.ToolCallback;
import org.springframework.http.MediaType;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import reactor.core.publisher.Flux;

import java.io.IOException;

/**
 * ai的对外服务接口控制器
 */
@RestController
@RequestMapping("/chat")
//@Tag(description = "ai服务接口", name = "ai服务接口")
public class AiController {

    @Resource
    private OnlineLearnApp onlineLearnApp;

    @Resource
    private ToolCallback[] allTools;

    @Resource
    private ChatModel dashscopeChatModel;

    /**
     * 聊天
     *
     * @param message 用户的问题
     * @param chatId  会话id
     * @return
     */
    @GetMapping("/learn_app/chat/sync")
    public String doChatWithLoveAppSync(String message, String chatId) {
        return onlineLearnApp.doChat(message, chatId);
    }

    /**
     * 流式输出聊天  方式一
     *
     * @param message
     * @param chatId
     * @return
     */
    @GetMapping(value = "/learn_app/ai/sse", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> doChatWithLearnAppAsy(String message, String chatId) {
        return onlineLearnApp.doChatByStream(message, chatId);
    }

    /**
     * 流式输出聊天  方式二
     *
     * @param message 用户的问题
     * @param chatId  会话id
     * @return
     */
    @GetMapping("/learn_app/chat/sse")
    public Flux<ServerSentEvent<String>> doChatWithLearnAppSSE(String message, String chatId) {
        return onlineLearnApp.doChatByStream(message, chatId)
                .map(chunk -> ServerSentEvent.<String>builder().data(chunk).build());
    }

    @GetMapping("/learn_app/chat/sse_Emitter")
    public SseEmitter doChatWithLearnAppSsEmitter(String message, String chatId) {
        SseEmitter sseEmitter = new SseEmitter(180000L);//三分钟的超时时间
        onlineLearnApp.doChatByStream(message, chatId)
                //处理每一条消息
                .subscribe(chunk -> {
                            try {
//                                //实时发送
//                                sseEmitter.send(chunk);
                                // 使用 SseEmitter.event() 构造符合 SSE 规范的数据
                                sseEmitter.send(SseEmitter.event()
                                        .data(chunk)
                                        .build());
                            } catch (IOException e) {
                                //处理异常
                                sseEmitter.completeWithError(e);
                            }
                        },
                        sseEmitter::completeWithError,//异常
                        sseEmitter::complete);//正常结束
        return sseEmitter;
    }


    /**
     * 携带 用户选择的 prompt
     *
     * @param message
     * @param chatId
     * @param promptId long型
     * @return
     */
    @GetMapping("/learn_app/chat/sse_Emitter/prompt")
    public SseEmitter doChatWithLearnAppSsEmitterByPrompt(String message, String chatId, Long promptId) {
        SseEmitter sseEmitter = new SseEmitter(180000L);//三分钟的超时时间
        onlineLearnApp.doChatByStreamWithPrompt(message, chatId, promptId)
                //处理每一条消息
                .subscribe(chunk -> {
                            try {
//                                //实时发送
//                                sseEmitter.send(chunk);
                                // 使用 SseEmitter.event() 构造符合 SSE 规范的数据
                                sseEmitter.send(SseEmitter.event()
                                        .data(chunk)
                                        .build());
                            } catch (IOException e) {
                                //处理异常
                                sseEmitter.completeWithError(e);
                            }
                        },
                        sseEmitter::completeWithError,//异常
                        sseEmitter::complete);//正常结束
        return sseEmitter;
    }

    /**
     * 携带 用户选择的 prompt 根据知识库回答用户问题
     *
     * @param message
     * @param chatId
     * @param promptId long型
     * @return
     */
    @GetMapping("/learn_app/chat/sse_Emitter/prompt/rag")
    public SseEmitter doChatByPromptWithRAG(String message, String chatId, Long promptId) {
        SseEmitter sseEmitter = new SseEmitter(180000L);//三分钟的超时时间
        onlineLearnApp.doChatByStreamWithPromptAndRAG(message, chatId, promptId)
                //处理每一条消息
                .subscribe(chunk -> {
                            try {
//                                //实时发送
//                                sseEmitter.send(chunk);
                                // 使用 SseEmitter.event() 构造符合 SSE 规范的数据
                                sseEmitter.send(SseEmitter.event()
                                        .data(chunk)
                                        .build());
                            } catch (IOException e) {
                                //处理异常
                                sseEmitter.completeWithError(e);
                            }
                        },
                        sseEmitter::completeWithError,//异常
                        sseEmitter::complete);//正常结束
        return sseEmitter;
    }
}
